/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Editar_empleado.java
 *
 * Created on 5/07/2011, 05:36:35 PM
 */
package curd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Javier
 */
public class Editar_empleado extends javax.swing.JFrame {

    /** Creates new form Editar_empleado */
    public Editar_empleado() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        e_idempleado = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        l_idempleado = new javax.swing.JLabel();
        e_nombre = new javax.swing.JTextField();
        e_apellido = new javax.swing.JTextField();
        l_apellido = new javax.swing.JLabel();
        l_nombre = new javax.swing.JLabel();
        rb_tipo1 = new javax.swing.JRadioButton();
        rb_soltero = new javax.swing.JRadioButton();
        rb_casado = new javax.swing.JRadioButton();
        e_direccion = new javax.swing.JTextField();
        e_puesto = new javax.swing.JTextField();
        l_puesto = new javax.swing.JLabel();
        l_tipo_contrato = new javax.swing.JLabel();
        rb_tipo2 = new javax.swing.JRadioButton();
        l_estado = new javax.swing.JLabel();
        l_direccion = new javax.swing.JLabel();
        boton_actualiza = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(curd.CURDApp.class).getContext().getResourceMap(Editar_empleado.class);
        e_idempleado.setText(resourceMap.getString("e_idempleado.text")); // NOI18N
        e_idempleado.setName("e_idempleado"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        l_idempleado.setText(resourceMap.getString("l_idempleado.text")); // NOI18N
        l_idempleado.setName("l_idempleado"); // NOI18N

        e_nombre.setEditable(false);
        e_nombre.setToolTipText(resourceMap.getString("e_nombre.toolTipText")); // NOI18N
        e_nombre.setName("e_nombre"); // NOI18N

        e_apellido.setEditable(false);
        e_apellido.setName("e_apellido"); // NOI18N

        l_apellido.setText(resourceMap.getString("l_apellido.text")); // NOI18N
        l_apellido.setName("l_apellido"); // NOI18N

        l_nombre.setText(resourceMap.getString("l_nombre.text")); // NOI18N
        l_nombre.setName("l_nombre"); // NOI18N

        rb_tipo1.setText(resourceMap.getString("rb_tipo1.text")); // NOI18N
        rb_tipo1.setName("rb_tipo1"); // NOI18N

        rb_soltero.setText(resourceMap.getString("rb_soltero.text")); // NOI18N
        rb_soltero.setName("rb_soltero"); // NOI18N
        rb_soltero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_solteroActionPerformed(evt);
            }
        });

        rb_casado.setText(resourceMap.getString("rb_casado.text")); // NOI18N
        rb_casado.setName("rb_casado"); // NOI18N
        rb_casado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_casadoActionPerformed(evt);
            }
        });

        e_direccion.setName("e_direccion"); // NOI18N

        e_puesto.setName("e_puesto"); // NOI18N

        l_puesto.setText(resourceMap.getString("l_puesto.text")); // NOI18N
        l_puesto.setName("l_puesto"); // NOI18N

        l_tipo_contrato.setText(resourceMap.getString("l_tipo_contrato.text")); // NOI18N
        l_tipo_contrato.setName("l_tipo_contrato"); // NOI18N

        rb_tipo2.setText(resourceMap.getString("rb_tipo2.text")); // NOI18N
        rb_tipo2.setName("rb_tipo2"); // NOI18N

        l_estado.setText(resourceMap.getString("l_estado.text")); // NOI18N
        l_estado.setName("l_estado"); // NOI18N

        l_direccion.setText(resourceMap.getString("l_direccion.text")); // NOI18N
        l_direccion.setName("l_direccion"); // NOI18N

        boton_actualiza.setText(resourceMap.getString("boton_actualiza.text")); // NOI18N
        boton_actualiza.setName("boton_actualiza"); // NOI18N
        boton_actualiza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_actualizaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(l_idempleado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addComponent(e_idempleado, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jButton1)
                        .addGap(26, 26, 26))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l_nombre)
                            .addComponent(l_apellido))
                        .addGap(71, 71, 71)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(e_apellido, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(e_nombre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                        .addGap(59, 59, 59))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l_direccion)
                            .addComponent(l_estado)
                            .addComponent(l_puesto)
                            .addComponent(l_tipo_contrato))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rb_tipo1)
                                .addGap(69, 69, 69)
                                .addComponent(rb_tipo2))
                            .addComponent(e_direccion, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rb_soltero)
                                .addGap(65, 65, 65)
                                .addComponent(rb_casado))
                            .addComponent(e_puesto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(boton_actualiza, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(40, 40, 40))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_idempleado)
                    .addComponent(jButton1)
                    .addComponent(e_idempleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_nombre)
                    .addComponent(e_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_apellido)
                    .addComponent(e_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_direccion)
                    .addComponent(e_direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_estado)
                    .addComponent(rb_soltero)
                    .addComponent(rb_casado))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_puesto)
                    .addComponent(e_puesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_tipo_contrato)
                    .addComponent(rb_tipo1)
                    .addComponent(rb_tipo2))
                .addGap(51, 51, 51)
                .addComponent(boton_actualiza)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
Cjempleado empleado = null;
String fecha = "";
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            Query q = session.createQuery("select from Cjempleado e where e.idEmpleado = " + e_idempleado.getText());
            List lista = q.list();
            Vector<Object> oneRow = null;
            for(Object o : lista) 
            {
                
                empleado = new Cjempleado((Cjempleado)o);
                oneRow = new Vector<Object>();
                oneRow.add(empleado.getNombres());
                oneRow.add(empleado.getApellidos());
                oneRow.add(empleado.getDireccion());
                oneRow.add(empleado.getEstadoCivil());
                oneRow.add(empleado.getPuesto());
                oneRow.add(empleado.getContratoTipo());
                oneRow.add(empleado.getFechaNac());
            }
            e_nombre.setText(oneRow.get(0).toString());
            e_apellido.setText(oneRow.get(1).toString());
            e_direccion.setText(oneRow.get(2).toString());
            if(oneRow.get(3).toString() == "true")
            {
                rb_casado.setSelected(true);
                rb_soltero.setSelected(false);
            }else
            {
                rb_soltero.setSelected(true);
                rb_casado.setSelected(false);
            }
            e_puesto.setText(oneRow.get(4).toString());
            if(oneRow.get(5) == 0)
            {
                rb_tipo1.setSelected(true);
                rb_tipo2.setSelected(false);
            }else
            {
                rb_tipo2.setSelected(true);
                rb_tipo1.setSelected(false);
            }
            fecha = oneRow.get(6).toString();
            JOptionPane.showMessageDialog(null, fecha);
        }
        catch(HibernateException he)
        {
        
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void rb_solteroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_solteroActionPerformed
        rb_casado.setSelected(false);
    }//GEN-LAST:event_rb_solteroActionPerformed

    private void rb_casadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_casadoActionPerformed
        rb_soltero.setSelected(false);
    }//GEN-LAST:event_rb_casadoActionPerformed

    private void boton_actualizaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_actualizaActionPerformed
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        try
        {
            Transaction trans = session.beginTransaction();
//            Cjempleado empleado = new Cjempleado();
//            Cjempleado empleado = (Cjempleado) session.load(Cjempleado.class, e_idempleado.getText());
            empleado.setDireccion(e_direccion.getText());
            empleado.setPuesto(e_puesto.getText());
            if(rb_soltero.isSelected() == true)
            {
                    empleado.setEstadoCivil(false);
            }          
            else
            {
                empleado.setEstadoCivil(true);                
            }
            byte y  = 0;
            if (rb_tipo1.isSelected()==true)
               empleado.setContratoTipo(y);
            else  
            {
                y = (byte) (y+1);
                empleado.setContratoTipo(y);
            }
            Date d = new Date();
            SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy"); 
            try {
                d = sdf.parse(fecha);
            } catch (ParseException ex) {
                Logger.getLogger(Editar_empleado.class.getName()).log(Level.SEVERE, null, ex);
            }
                empleado.setFechaNac(d);
            
            session.update(empleado);
            trans.commit();
            session.close();
            JOptionPane.showMessageDialog(null, "datos actualizados exitosamente");
  
        }
        catch(HibernateException he)
        {
            he.printStackTrace();
            JOptionPane.showMessageDialog(null, "no se pudo");
        }
    }//GEN-LAST:event_boton_actualizaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Editar_empleado().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_actualiza;
    private javax.swing.JTextField e_apellido;
    private javax.swing.JTextField e_direccion;
    private javax.swing.JTextField e_idempleado;
    private javax.swing.JTextField e_nombre;
    private javax.swing.JTextField e_puesto;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel l_apellido;
    private javax.swing.JLabel l_direccion;
    private javax.swing.JLabel l_estado;
    private javax.swing.JLabel l_idempleado;
    private javax.swing.JLabel l_nombre;
    private javax.swing.JLabel l_puesto;
    private javax.swing.JLabel l_tipo_contrato;
    private javax.swing.JRadioButton rb_casado;
    private javax.swing.JRadioButton rb_soltero;
    private javax.swing.JRadioButton rb_tipo1;
    private javax.swing.JRadioButton rb_tipo2;
    // End of variables declaration//GEN-END:variables
}
